package com.wen.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wen.mapper.RoleAuthMapper;
import com.wen.mapper.UserRoleMapper;
import com.wen.pojo.Auth;
import com.wen.pojo.CurrentUser;
import com.wen.pojo.Role;
import com.wen.pojo.RoleAuth;
import com.wen.utils.PageBean;
import com.wen.utils.Result;
import com.wen.utils.ThreadLocalUtil;
import dto.AuthGrandDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.wen.mapper.RoleMapper;
import com.wen.service.RoleService;

import java.util.List;

@CacheConfig(cacheNames = "com.wen.service.impl.RoleServiceImpl")
@Service
public class RoleServiceImpl implements RoleService{

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Cacheable(key = "'all:role'")
    @Override
    public List<Role> allRoleList() {
        return roleMapper.allRoleList();
    }

    @Override
    public List<Role> findUserRoleById(Integer userId) {
        return roleMapper.findUserRoleById(userId);
    }

    @Override
    public PageBean<Role> allRolePageList(Integer pageNum, Integer pageSize, Role role) {
        PageBean<Role> pb = new PageBean<>();
        PageHelper.startPage(pageNum,pageSize);
        List<Role> as = roleMapper.allRolePage(role);

        Page<Role> p = (Page<Role>) as;

        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public Result addRole(Role role) {
        Role r = roleMapper.findRoleByNameCode(role.getRoleName(), role.getRoleCode());
        if(r == null){
            CurrentUser currentUser = ThreadLocalUtil.get();
            int userId = currentUser.getUserId();
            role.setCreateBy(userId);
            int i = roleMapper.addRole(role);
            if(i>0){
                stringRedisTemplate.delete("com.wen.service.impl.RoleServiceImpl::all:role");
                return Result.ok(200,"添加成功",null);

            }else{
                return Result.fail(1,"添加失败");
            }
        }else{
            return Result.fail(1,"重复");
        }

    }

    @Override
    public Result updRoleState(Role role) {
        int i =  roleMapper.updRoleState(role.getRoleId(),role.getRoleState());
        if(i>0){
            stringRedisTemplate.delete("com.wen.service.impl.RoleServiceImpl::all:role");
            return Result.ok(200,"成功",null);

        }else{
            return Result.fail(1,"失败");
        }
    }

    @Autowired
    private RoleAuthMapper roleAuthMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Override
    public Result delRole(Integer roleId) {
        int i = roleMapper.delRole(roleId);
        if(i>0){
            stringRedisTemplate.delete("com.wen.service.impl.RoleServiceImpl::all:role");
            roleAuthMapper.delRoleAuth(roleId);
            userRoleMapper.delRoleById(roleId);
            return Result.ok(200,"成功",null);
        }else{
            return Result.fail(1,"失败");
        }
    }

    @Override
    public List<Integer> findAllFpListById(Integer roleId) {
        return  roleMapper.findAllFpListById(roleId);
    }

    @Override
    public Result authGrant(AuthGrandDto authGrandDto) {

        roleAuthMapper.delRoleAuth(authGrandDto.getRoleId());
        List<Integer> authIds = authGrandDto.getAuthIds();
        System.out.println(authIds);
        for (Integer authId : authIds){
            roleAuthMapper.addRoleAuth(new RoleAuth(null,authGrandDto.getRoleId(),authId));
        }
        //更新菜单清空redis缓存
        stringRedisTemplate.delete("authTree:all");
        CurrentUser currentUser = ThreadLocalUtil.get();
        int userId = currentUser.getUserId();
        stringRedisTemplate.delete("authTree:"+userId);
        return Result.ok(200,"添加成功",null);
    }

    @Override
    public Result updRole(Role role) {
        CurrentUser currentUser = ThreadLocalUtil.get();
        int userId = currentUser.getUserId();
        role.setUpdateBy(userId);
        int i = roleMapper.updRole(role);
        if(i>0){
            return Result.ok(200,"成功",null);

        }else{
            return Result.fail(1,"失败");
        }
    }

}
